<?php
/**
 * userLoginDao
 */

class Dao_UserConsumeModel extends Dao_BaseModel {

    protected $_table = 'user_consume';

    const NORMAL_STATUS = 1;
    const REDIS_KEYWORD = 'USER_SET_KEY_';
    const EXTIME        = '86400';

    public function __construct ()
    {
        parent::__construct ();
    }
    
    /**
     * 获取用户充值记录
     */
    public function getUserConsume( $uid )
    {
        if( !$uid ){
            return [];
        }
        $where   = [];
        $where[] = [ 'user_id' , $uid ];
        $order   = [ 'pay_time' => 'desc' ];
        $info = parent::getRows( array ( '*' ) , $where ,$order );
        return $info;
    }
    
    /**
     * 获取用户某本小说充值记录
     */
    public function getUserNovelConsume( $uid ,$novelid ,$chapterid )
    {
        if( !$uid && ! $novelid){
            return [];
        }
        $where   = [];
        $where[] = [ 'user_id' , $uid ];
        $where[] = [ 'novel_id' , $novelid ];
        $where[] = [ 'novel_chapter' , $chapterid ];
        $order   = [ 'pay_time' => 'desc' ];
        $info = parent::getRows( array ( '*' ) , $where ,$order );
        return $info;
    }
    
    /**
     * 新增记录
     */
    public function addConsume($data)
    {
        $info = parent::add ( $data );
        return $info;
    }
    
    /**
     * 获取当天购买次数最多的小说
     */
    public function getTodayNovel( $type = 1 )
    {
        $where      = "";
        $where     .= " status = ".self::NORMAL_STATUS." ";
        $now        = date( 'Y-m-d H:i:s' );
        $year       = date('Y');
        $month_n    = date('n');
        $day_j      = date('j');
        $week_day   = date('w');
        switch( $type ){
            case 1:  //日榜
                $yestoday = date( 'Y-m-d 00:00:00' , strtotime('-1 day') );
                $tomorrow = date( 'Y-m-d 00:00:00' , strtotime('+1 day') );
                $where  .= " and pay_time >= '$yestoday' ";
                $where  .= " and pay_time < '$tomorrow' ";
                break;
            case 2:  //周榜
                if( $week_day == 0 || $week_day == 1 ){
                    $where  .= " and  pay_time >= '".date( 'Y-m-d 00:00:00' , strtotime('-7 days') )."' ";
                    $where  .= " and pay_time <='$now' ";
                }else{
                    $where  .= " and  pay_time >= '".date( 'Y-m-d 00:00:00' , time() - ( $week_day - 1 ) * 24 * 3600 )."' ";
                    $where  .= " and pay_time <='$now' ";
                }
                break;
            case 3:  //月榜
                if( $day_j == 1 ){
                    if( $month_n == 1 ){
                        $last_year = $year - 1;
                        $where  .= " and  pay_time >= '".$last_year.'-12-01 00:00:00'."' ";
                        $where  .= " and pay_time <='$now' ";
                    }else{
                        $last_month = date( 'Y-m-01 00:00:00' , strtotime('-1 month') );
                        $where  .= " and pay_time >= '$last_month' ";
                        $where  .= " and pay_time <= '$now' ";
                    }
                }else{
                    $where  .= " and pay_time >= '".date( 'Y-m-01 00:00:00' )."' ";
                    $where  .= " and pay_time <= '$now' ";
                }
                break;
            default :
                break;
        }
        $sql = "select count(*) as cnt,novel_id from xs_user_consume where $where group by novel_id order by cnt desc,novel_id desc";
        $info = $this->query($sql);
        return $info;
    }
    
}
